<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>

<body>
	<?php 		
		$theme=$_POST['theme'];		
		$date=date('y-m-d');
		session_start();
		$user_name=$_SESSION['user_name'];
		//发表留言			
		if(isset($_POST['text'])){			
			$parent="0";
			$content=$_POST['text'];
		}
		//发表评论
		else{
			$parent=$user_name;
			$content=$_POST['textarea'];
		}
		$conn=mysql_connect("localhost","root","root") or die("连接数据库失败！");
		mysql_select_db("guestbook") or die("选择数据库失败！");
		mysql_query("set names 'utf8'");
		mysql_query("set character set 'utf8'");
		if($theme==""){
			echo "<script>alert('主题不能为空！');
			window.location.href='../view/leaveComments.php';</script>";
		}
		else if($sqll=mysql_fetch_array(mysql_query("select *from stateinfo where theme='$theme'"))&&$parent==null){
			echo "<script>alert('该主题已存在！');
			window.location.href='../view/leaveComments.php';</script>";
		}else{
		  $sql=mysql_query("insert into stateinfo  values(null,'$user_name','$theme','$content','$date','$parent');");echo $parent;
		  if($sql){
			  echo "<script>alert('发言成功！');
			  window.location.href='../view/allComments.php';</script>";
		  }else{
			  echo "<script>alert('发言失败！');
			  window.location.href='../view/leaveComments.php';</script>";
		  }
		}
		mysql_close($conn);
 	?>
			
</body>
</html>